/*
 * $Id: ShowResourceInHistoryAction.java 488 2008-08-20 16:59:47Z rcleveng $
 * 
 * Copyright (c) 2001,2002 Boris Pruessmann and others. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Common Public License v0.5 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors: Boris Pruessmann - Initial implementation.
 */
package net.sourceforge.perforce.ui.action;

import net.sourceforge.perforce.core.PerforceCorePlugin;
import net.sourceforge.perforce.core.internal.P4TeamProvider;
import net.sourceforge.perforce.ui.views.HistoryView;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.team.core.RepositoryProvider;

/**
 * @version $Revision: 488 $
 * @author <a href="mailto:bpruessmann@users.sourceforge.net">Boris Pr??mann</a>
 */
public class ShowResourceInHistoryAction extends PerforceAction {
  //----------------------------------------------------------------------------
  // -- Public Methods

  @Override
  public void run(IAction action) {
    run(new IRunnableWithProgress() {
      public void run(IProgressMonitor monitor) {
        IResource[] resources = getSelectedResources();
        if (resources.length != 1) {
          return;
        }

        HistoryView view = HistoryView.openInActivePerspective();
        if (view != null) {
          view.showHistory(resources[0]);
        }
      }
    }, PROGRESS_BUSYCURSOR);
  }

  // ---------------------------------------------------------------------------
  // Protected Methods

  @Override
  protected boolean isEnabled() {
    IResource[] resources = getSelectedResources();
    if (resources.length != 1) {
      return false;
    }

    IResource resource = resources[0];
    if (!(resource instanceof IFile)) {
      return false;
    }

    RepositoryProvider tempProvider = RepositoryProvider.getProvider(resource.getProject());
    if (tempProvider == null || !tempProvider.getID().equals(PerforceCorePlugin.getTypeId())) {
      return false;
    }

    P4TeamProvider provider = (P4TeamProvider) tempProvider;
    if (!(provider.hasRemote(resource))) {
      return false;
    }

    return true;
  }
}
